package pwd.allen.bytebuddy;

import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.utility.JavaModule;
import pwd.allen.intercept.MyInterceptor;

public class AgentTransformer implements AgentBuilder.Transformer {
    /**
     * @param builder
     * @param typeDescription 表示要被加载的类的信息
     * @param classLoader
     * @param javaModule
     * @return
     */
    @Override
    public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, JavaModule javaModule) {
        // method指定哪些方法需要被拦截，ElementMatchers.any指定了所有的方法
        return builder.method(ElementMatchers.<MethodDescription>any())
                .intercept(MethodDelegation.to(MyInterceptor.class));   // 方法委托给拦截器进行增强处理
    }
}
